﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace WaferImage
{
    [SerializableAttribute(), TypeConverter(typeof(ExpandableObjectConverter))]
    public class Config
    {        
        [DisplayName("管理员"),CategoryAttribute("Admin"), ReadOnly(true), DescriptionAttribute("配置的最后修改时间")]
        public DateTime FileTime        
        {
            get { return fileTime; }
            set { fileTime = value; }
        }
        private DateTime fileTime;
       
        #region 线痕图片
        [DisplayName("标准灰度值下限"), Category("线痕图片"), Description("标准灰度值下限")]
        public float SawCheckGrayLow { get { return sawCheckGrayLow; } set { sawCheckGrayLow = value; } }
        private float sawCheckGrayLow = 100F;
        [DisplayName("标准灰度值上限"), Category("线痕图片"), Description("标准灰度值上限")]
        public float SawCheckGrayUp { get { return sawCheckGrayUp; } set { sawCheckGrayUp = value; } }
        private float sawCheckGrayUp = 100F;

        [DisplayName("标准长宽比下限"), Category("线痕图片"), Description("标准长宽比下限")]
        public float SawCheckWidthLow { get { return sawCheckWidthLow; } set { sawCheckWidthLow = value; } }
        private float sawCheckWidthLow = 0.3F;
        [DisplayName("标准长宽比上限"), Category("线痕图片"), Description("标准长宽比上限")]
        public float SawCheckWidthUp { get { return sawCheckWidthUp; } set { sawCheckWidthUp = value; } }
        private float sawCheckWidthUp = 0.3F;
        #endregion 线痕图片

        #region 前后左右侧崩图片
        [DisplayName("标准灰度值下限"), Category("前后左右侧崩图片"), Description("标准灰度值下限")]
        public float LrFbChipsCheckGrayLow { get { return lrFbChipsCheckGrayLow; } set { lrFbChipsCheckGrayLow = value; } }
        private float lrFbChipsCheckGrayLow = 200F;

        [DisplayName("标准灰度值上限"), Category("前后左右侧崩图片"), Description("标准灰度值上限")]
        public float LrFbChipsCheckGrayUp { get { return lrFbChipsCheckGrayUp; } set { lrFbChipsCheckGrayUp = value; } }
        private float lrFbChipsCheckGrayUp = 200F;

        [DisplayName("清晰度下限"), Category("前后左右侧崩图片"), Description("清晰度下限")]
        public float LrFbChipsCheckFactorLow { get { return lrFbChipsCheckFactorLow; } set { lrFbChipsCheckFactorLow = value; } }
        private float lrFbChipsCheckFactorLow = 200F;

        [DisplayName("清晰度上限"), Category("前后左右侧崩图片"), Description("清晰度上限")]
        public float LrFbChipsCheckFactorUp { get { return lrFbChipsCheckFactorUp; } set { lrFbChipsCheckFactorUp = value; } }
        private float lrFbChipsCheckFactorUp = 200F;

        #endregion 前后左右侧崩图片

        #region 隐裂图片

        [DisplayName("各区域灰度值下限"), Category("隐裂图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("各区域灰度值下限")]
        public Gray CracksGrayLow { get { return cracksGrayLow; } set { cracksGrayLow = value; } }
        private Gray cracksGrayLow = new Gray();

        [DisplayName("各区域灰度值上限"), Category("隐裂图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("各区域灰度值上限")]
        public Gray CracksGrayUp { get { return cracksGrayUp; } set { cracksGrayUp = value; } }
        private Gray cracksGrayUp = new Gray();

        [DisplayName("拉普拉斯值"), Category("隐裂图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("拉普拉斯值,得分越高图像越清晰")]
        public double CracksLaplacian { get { return cracksLaplacian; } set { cracksLaplacian = value; } }
        private double cracksLaplacian=8;

        [DisplayName("图像畸变比例"), Category("隐裂图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("图像畸变比例,值越大,图像越好")]
        public double CracksDistortion { get { return cracksDistortion; } set { cracksDistortion = value; } }
        private double cracksDistortion=0.98;

        [DisplayName("sobel值"), Category("隐裂图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("sobel值,得分越高图像越清晰")]
        public double CracksSobel { get { return cracksSobel; } set { cracksSobel = value; } }
        private double cracksSobel=3.5;

        [DisplayName("标准差"), Category("隐裂图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("标准差,值越大,差异性越大,图像越清晰")]
        public double CracksSdev { get { return cracksSdev; } set { cracksSdev = value; } }
        private double cracksSdev=5;

        #endregion 隐裂图片

        #region 表崩图片

        [DisplayName("各区域灰度值下限"), Category("表崩图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("各区域灰度值下限")]
        public Gray ChipsGrayLow { get { return chipsGrayLow; } set { chipsGrayLow = value; } }
        private Gray chipsGrayLow = new Gray();

        [DisplayName("各区域灰度值上限"), Category("表崩图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("各区域灰度值上限")]
        public Gray ChipsGrayUp { get { return chipsGrayUp; } set { chipsGrayUp = value; } }
        private Gray chipsGrayUp = new Gray();

        [DisplayName("拉普拉斯值"), Category("表崩图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("拉普拉斯值,得分越高图像越清晰")]
        public double ChipsLaplacian { get { return chipsLaplacian; } set { chipsLaplacian = value; } }
        private double chipsLaplacian =8;

        [DisplayName("图像畸变比例"), Category("表崩图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("图像畸变比例,值越大,图像越好")]
        public double ChipsDistortion { get { return chipsDistortion; } set { chipsDistortion = value; } }
        private double chipsDistortion=0.98;

        [DisplayName("sobel值"), Category("表崩图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("sobel值,得分越高图像越清晰")]
        public double ChipsSobel { get { return chipsSobel; } set { chipsSobel = value; } }
        private double chipsSobel=3.5;

        [DisplayName("标准差"), Category("表崩图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("标准差,值越大,差异性越大,图像越清晰")]
        public double ChipsSdev { get { return chipsSdev; } set { chipsSdev = value; } }
        private double chipsSdev=5;

        #endregion 表崩图片

        #region 脏污图片

        [DisplayName("各区域灰度值下限"), Category("脏污图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("各区域灰度值下限")]
        public Gray StainsGrayLow { get { return stainsGrayLow; } set { stainsGrayLow = value; } }
        private Gray stainsGrayLow = new Gray();

        [DisplayName("各区域灰度值上限"), Category("脏污图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("各区域灰度值上限")]
        public Gray StainsGrayUp { get { return stainsGrayUp; } set { stainsGrayUp = value; } }
        private Gray stainsGrayUp = new Gray();

        [DisplayName("拉普拉斯值"), Category("脏污图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("拉普拉斯值,得分越高图像越清晰")]
        public double StainsLaplacian { get { return stainsLaplacian; } set { stainsLaplacian = value; } }
        private double stainsLaplacian=8;

        [DisplayName("图像畸变比例"), Category("脏污图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("图像畸变比例,值越大,图像越好")]
        public double StainsDistortion { get { return stainsDistortion; } set { stainsDistortion = value; } }
        public double stainsDistortion=0.96;

        [DisplayName("sobel值"), Category("脏污图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("sobel值,得分越高图像越清晰")]
        public double StainsSobel { get { return stainsSobel; } set { stainsSobel = value; } }
        private double stainsSobel=3.5;

        [DisplayName("标准差"), Category("脏污图片"), TypeConverter(typeof(ExpandableObjectConverter)), Description("标准差,值越大,差异性越大,图像越清晰")]
        public double StainsSdev { get { return stainsSdev; } set { stainsSdev = value; } }
        private double stainsSdev=5;

        #endregion 表崩图片



        public string Save()
        {
            
            try
            {
                fileTime = DateTime.Now;
                Type thisType = this.GetType();
                XmlSerializer serializer = new XmlSerializer(thisType);


                FileStream fs = new FileStream("Config.xml", FileMode.Create);
                TextWriter writer = new StreamWriter(fs, new System.Text.UTF8Encoding());
                
                serializer.Serialize(writer, this);
                writer.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error Saving Configuration", MessageBoxButtons.OK);
            }
            return "";
        }

        public string Load(ref Config cfg) 
        {            
            string errs = "";
            try
            {
                FileInfo fi = new FileInfo("Config.xml");
                if (!fi.Exists)
                {
                    cfg.Save(); // File doesn't exist, write a new one..
                    return "";
                }
                else if (fi.IsReadOnly)
                {
                    fi.Attributes = fi.Attributes ^ FileAttributes.ReadOnly; // Clear the Readonly flag.
                }
                Type thisType = cfg.GetType();
                XmlSerializer serializer = new XmlSerializer(thisType);

                
                FileStream fs = new FileStream("Config.xml", FileMode.Open);
                TextReader reader = new StreamReader(fs, new System.Text.UTF8Encoding());
                
                cfg=(Config)serializer.Deserialize(reader);
                
                reader.Close();
            }
            catch (Exception ex)
            {
                errs = "Exception Loading System Configuration File:  " + ex.Message;
            }
            return errs;
        }
      
    }
}
